/*
 * stnewsletter
 *
 * http://www.pluginexpert.com
 */
var stTwitterToolbox = {
    loadPage: function(page_name, container_id, action, data_post, message, callback_function) {
        var post_data = "page="+page_name+"&ajax=1&action="+action;
        if(data_post != '' || data_post != null){
            for( var key in data_post){
                post_data +="&"+key+"="+data_post[key];
            }
        }
        //alert(post_data);
        jQuery.ajax({
            type: "POST",
            //url: "admin.php?page=home",
            data: post_data,
            cache: false,
            beforeSend: function(){
                jQuery("#"+container_id+" .loading").fadeIn("fast");
            }, //show loading just when link is clicked
            complete: function(){
                jQuery("#"+container_id+" .loading").fadeOut("fast");
            }, //stop showing loading when the process is complete
            success: function(data){
                jQuery("#"+container_id).html(data);
                //jQuery("#"+container_id).append('<input type="hidden" value="'+action+'" id="current_action"');
                if(message){
                    jQuery('#message').html('<p>'+message+'</p>');
                    jQuery('#message').fadeIn('slow');
                }
                if(callback_function){
                    callback_function.call(this, data_post);
                }
            }
        });
    },
    load_tooltip: function(id){
        jQuery(document).ready(function() {
            if(id == '')
                jQuery("#inner_content").tooltip();
            else
                jQuery(id).tooltip();
        });
    },

    on_submitform: function(form, callback_function){

        jQuery(document).ready(function(){
            jQuery(form+' input').each(function(){
                jQuery(this).focus(function(){
                    jQuery(this).parent().addClass('focus');
                });
                jQuery(this).blur(function(){
                    jQuery(this).parent().removeClass('focus');
                });
            });
            jQuery(form+' textarea').each(function(){
                jQuery(this).focus(function(){
                    jQuery(this).parent().addClass('focus');
                });
                jQuery(this).blur(function(){
                    jQuery(this).parent().removeClass('focus');
                });
            });
            jQuery(form+' select').each(function(){
                jQuery(this).focus(function(){
                    jQuery(this).parent().addClass('focus');
                });
                jQuery(this).blur(function(){
                    jQuery(this).parent().removeClass('focus');
                });
            });
            jQuery(form).css('position', 'relative');
            jQuery(form).append('<div class="loading"></div>');

            jQuery(form).append(
                '<div class="notification">'
                +'<span></span>'
                +'<div class="text">'
                +'<p><strong></strong> '
                +'<span></span>'
                +'</p>'
                +'</div>'
                +'</div>'
                );
            jQuery(form).validate({
                errorPlacement: function(error, element) {
                //error.appendTo( element.parent());

                },
                submitHandler: function() {
                    jQuery( form +" .loading").show('slow');
                    jQuery.post(jQuery(form).attr('action')+'&ajax=1', jQuery(form).serialize(),function(data, textStatus){
                        if(jQuery.isFunction(callback_function)){
                                callback_function.call(this, data);
                            }else if(typeof(callback_function) == 'undefined'){
                            	stnewsletter.sendMessage(data);
                            } else {
                            	stnewsletter.sendMessage(data);
                            }
                            jQuery(".loading").hide('normal');
                    });
                }

            });
        });
    },

    sendMessage: function(data){
        jQuery(document).ready(function(){
            try{
                data = eval('('+data+')');
            }catch (e){}

            if(typeof(data)=='string'){
                var message = data;
                var status = 'message';
            }else{
                message = data['message'];
                status = data['status'];
            }
            if(status == 'success'){
                jQuery('.notification' ).removeClass('notification_error');
                jQuery('.notification' ).removeClass('notification_message');
                jQuery('.notification' ).addClass('notification_success');
                jQuery('.notification .text strong').html('Success!');
            }else if(status == 'message'){
                jQuery('.notification' ).removeClass('notification_error');
                jQuery('.notification' ).removeClass('notification_success');
                jQuery('.notification' ).addClass('notification_message');
                jQuery('.notification .text strong').html('Message!');
            }else{
                jQuery('.notification' ).removeClass('notification_message');
                jQuery('.notification' ).removeClass('notification_success');
                jQuery('.notification' ).addClass('notification_error');
                jQuery('.notification .text strong').html('Error!');
            }
            jQuery('.notification .text span').html(message);
            jQuery('.notification').fadeIn('slow');

            jQuery('.notification').click(function() {
                jQuery(this).fadeOut(800);
            });
            window.setTimeout(function(){
               jQuery('.notification').fadeOut();
            }, 2500);
        });

    }
};